package com.zz.framework.core.utils

import java.io.PrintWriter

/**
 * Copyright © 2025 zhun All rights reserved.
 * Created by ZhaoZhun on 2025/2/17 21:51.
 * @author: ZhaoZhun 1820022519@qq.com
 * @version: V1.0
 */
object ApkUtils {
    // 静默安装
    /**
     * 静默安装apk 需要获取 root 权限
     * @param apkPath String
     * @return Boolean
     *
     */
    fun clientInstall(apkPath: String): Boolean {
        var mPrintWriter: PrintWriter? = null
        var process: Process? = null
        try {
            process = Runtime.getRuntime().exec("su")
            mPrintWriter = PrintWriter(process.outputStream).apply {
                println("chmod 777 $apkPath")
                println("export LD_LIBRARY_PATH=/vendor/lib:/system/lib")
                //-r 表示重新安装应用，如果应用已安装，它会被替换。
                println("pm install -r $apkPath")
                flush()
                close()
            }
            val value = process.waitFor()
            return value == 1
        } catch (e: Exception) {
            e.printStackTrace()
        } finally {
            process?.destroy()
        }
        return false
    }

}